import { getApiClient } from '../utils/api';
export function useArticles22(initialParams = {}) {
  const state = reactive({
    articles: [],
    trendingArticles: [],
    authorRanks: [],
    pageNum: 1,
    total: 0,
    pageSize: 20,
    isLoading: false,
    isFinished: false,
    error: null
  });

  // 服务端渲染数据获取
  const { data, error } = useAsyncData(
      'articleDatas',
      async () => {
        try {
          state.isLoading = true

          // 修正拼写错误 response
          const response = await getApiClient().get('/app/article/list', {
            params: {
              ...initialParams,
              pageNum: state.pageNum,
              pageSize: state.pageSize
            }
          });
          if (response?.code === 200) {
            state.articles = response?.rows || []
            return {
              articles: response?.rows || [],
              total: response?.total || 0,
              trending: response?.trending || [],
              ranks: response?.authorRanks || []
            };
          }
          throw new Error(response?.message || 'API返回结构异常');

        } catch (err) {
          console.error('列表加载失败:', err);
          return {
            articles: [],
            total: 0,
            trending: [],
            ranks: []
          };
        }finally {
          state.isLoading = false
          state.isFinished =  state.total < state.pageNum * state.pageSize;
        }
      },
      {
        lazy: false, // 阻塞路由直到数据加载完成[1](@ref)
        server: true,
        // 添加以下配置
        transform: (response) => {
          if (response) {
            state.articles = response.articles
            state.total = response.total
            state.trendingArticles = response.trending
            state.authorRanks = response.ranks
            state.isFinished = response.total < state.pageNum * state.pageSize
          }
          return response

        },
        key: JSON.stringify(initialParams),
        retry: 3,
        // 添加缓存配置
        getCachedData: (key) => {
          // 实现缓存逻辑
          return null
        }

      }
  );

  // 加载更多
  const loadMore = async () => {
    if (state.isLoading || state.isFinished) return;
    state.pageNum++;
    state.isLoading = true;
    try {
      const response = await getApiClient().get('/app/article/list', {
        params: {
          ...initialParams,
          pageNum: state.pageNum,
          pageSize: state.pageSize
        }
      });

      if (response?.code === 200) {
        const newArticles = response?.rows || [];
        state.total =  response?.total || 0;
        state.articles.push(...newArticles);
        state.isFinished = state.total < state.pageNum * state.pageSize;
      }
    } catch (err) {
      state.error = err.message;
      state.pageNum--; // 出错时回退页码
    } finally {
      state.isLoading = false;
    }
  };

  return {
    articles : state.articles,
    isLoading : state.isLoading ? state.isLoading : false,
    isFinished : state.isFinished ? state.isFinished : false,
    loadMore
  };
}
